package com.woniuxy.commons.utile;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.FileInputStream;
import java.io.InputStream;

public class OssTemplate {

    private static String endpoint= "oss-cn-chengdu.aliyuncs.com";

    private static  String keyid="LTAI5tLgMscWPiP9mQ4Ln7rB";

    private static String ketSecret="XCzXKJa28n41F7MeHCLJ8TpYsMLnBm";


    //name:文件名 file:文件 存储空间名：bucke
    public static String fileUpload(String name, InputStream file,String buketname){
        OSS ossClient = new OSSClientBuilder().build("http://" + endpoint, keyid, ketSecret);
        String url=null;
        try {
            ossClient.putObject(buketname,name,file);
            url="http://"+buketname+"."+endpoint+"/"+name;
        }finally {
            if (ossClient!=null) ossClient.shutdown();
        }
        return url;
    }

    public static  boolean deletefile(String name,String buketname){
        OSS ossClient = new OSSClientBuilder().build("http://" + endpoint, keyid, ketSecret);
        try {
            ossClient.deleteObject(buketname,name);

        }finally {
            if (ossClient!=null) ossClient.shutdown();
        }
        return true;
    }

    public static void main(String[] args) throws  Exception{
        FileInputStream fileInputStream = new FileInputStream("C:\\imgs\\jj.jpg");
        String repairstage = fileUpload("yaoxin.png", fileInputStream, "repairstage");
        System.out.println(repairstage);
    }

}
